package com.vxb.testmvp.test_fragment;

import com.vxb.testmvp.test_fragment.fm_a.FragmentA;
import com.vxb.testmvp.test_fragment.fm_b.FragmentB;
import com.vxb.testmvp.test_fragment.fm_c.FragmentC;
import com.vxb.testmvp.test_fragment.fm_d.FragmentD;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by User on 2017/3/2.
 */

public class FragmentFactory {

    public static Map<Integer, BaseFragment> mFragments = new HashMap<Integer, BaseFragment>();

    public static BaseFragment createFragment(int position) {

        BaseFragment fragment = null;
        fragment = mFragments.get(position);  //在集合中取出来Fragment
        if (fragment == null) {  //如果在集合中没有取出来，需要重新创建

            if (position == 0) {
                fragment = new FragmentA();
            } else if (position == 1) {
                fragment = new FragmentB();
            } else if (position == 2) {
                fragment = new FragmentC();
            } else if (position == 3) {
                fragment = new FragmentD();
            }
            if (fragment != null) {
                mFragments.put(position, fragment);
            }
        }
        return fragment;
    }


}
